import { TimeManager } from "./TimeManager";

export abstract class TimeObserver {

    // 倒计时时间 单位秒  由于服务器返回的是秒，而时间管理器需要毫秒 所以注意时间转换。
    protected time: number = 0;

    //回调函数
    protected func: (time: number) => void;

    //time为0时 是否从时间管理器中移除。
    protected autoDelete: boolean = false;

    //间隔时间
    protected duration: number = 1;

    protected isPause: boolean = false;

    constructor(func: (time: number) => void, duration: number = 1) {
        this.init(func, duration)
    }

    init(func: (time: number) => void, duration: number = 1) {
        this.setCallback(func)
        this.setTime(0)
        this.setDuration(duration);
    }

    setCallback(func: (time: number) => void) {
        this.func = func
    }

    setTime(t: number, param?: any) {
        this.time = t;
    }

    setDuration(d: number) {
        this.duration = d;
    }

    getDuration() {
        return this.duration;
    }

    setAutoDelete(flag: boolean) {
        this.autoDelete = flag;
    }

    isAutoDelete() {
        return this.autoDelete;
    }

    getTime() {
        return this.time;
    }

    abstract check(dt: number): boolean;

    update(dt: number): boolean {
        if (this.isPause) {
            return false;
        }
        return this.check(dt)
    }

    pause() {
        this.isPause = true;
    }

    resume() {
        this.isPause = false;
    }

    start() {
        TimeManager.instance().add(this)
    }

    stop() {
        TimeManager.instance().remove(this)
    }

    clear() {

    }

}